from flask import Blueprint, jsonify, request, g
from app.err_no import errno_no
from loguru import logger
import json
import traceback
from app.db import dbStallManage
from app.db.mongoClient import MongoError

blueprint = Blueprint('stall_manage', __name__, url_prefix='/api/stall')


@blueprint.route('/v1/create', methods=['POST'])
def create():
    '''
    创建餐厅档口
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallName = j_data.get('stallName')
        stallDesc = j_data.get('stallDesc')
        if diningNo is None or len(diningNo) <= 0 \
            or stallName is None or len(stallName) <= 0 \
            or stallDesc is None:
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStallManage.createStall(diningNo, stallName, stallDesc, g.userId)
        if res is None:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))

        #该餐厅的该档口存在
        if res is False:
            return jsonify(errno_no.ErrServerDocumentExist.data(None))

        return jsonify(errno_no.OK.data({'stallNo': res}))
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))


@blueprint.route('/v1/edit', methods=['POST'])
def stall_edit():
    '''
    编辑档口
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        stallName = j_data.get('stallName')
        stallDesc = j_data.get('stallDesc')
        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 \
            or stallName is None or len(stallName) <= 0 \
            or stallDesc is None:
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStallManage.editStall(diningNo, stallNo, stallName, stallDesc, g.userId)
        if res is None:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))

        return jsonify(errno_no.OK.data(None))
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))


@blueprint.route('/v1/del', methods=['POST'])
def stall_del():
    '''
    删除档口
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')

        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 :
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStallManage.delStall(diningNo, stallNo)
        if res is None:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))
        
        # 如果该餐厅该档口下有菜则不让删除
        if res is False:
            return jsonify(errno_no.ErrStallDelForbid.data(None))
        
        return jsonify(errno_no.OK.data(None))
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))


@blueprint.route('/v1/pos_report', methods=['POST'])
def pos_report():
    '''
    档口点餐机信息上报
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        deviceNo = j_data.get('deviceNo')
        deviceType = j_data.get('deviceType')
        softVersion = j_data.get('softVersion')
        skj130No = j_data.get('skj130No')
        if diningNo is None or stallNo is None or skj130No is None or deviceNo is None or deviceType is None or softVersion is None or skj130No is None:
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStallManage.posReport(diningNo, stallNo, deviceNo, deviceType, softVersion, skj130No)
        if res is None:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))

        return jsonify(errno_no.OK.data(None))
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))


@blueprint.route('/v1/pos_setUpdateStatus', methods=['POST'])
def pos_setUpdateStatus():
    '''
    点餐机升级设置
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        deviceNo = j_data.get('deviceNo')
        updateStatus = j_data.get('updateStatus')
        
        if diningNo is None or stallNo is None or deviceNo is None or updateStatus is None:
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStallManage.posSetUpdateStatus(diningNo, stallNo, deviceNo, updateStatus)
        if res is None:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))

        return jsonify(errno_no.OK.data(None))
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
